/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.backend;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cholm
 *         <p/>
 *         A sub-category
 */
public class SubCategory {

    /**
     * Name of sub category
     */
    protected String mName = "";
    /**
     * URL to load from
     */
    protected String mUrl = "";
    /**
     * Parent category
     */
    protected Category mParent = null;
    /**
     * Flag signaling if we're loaded or not
     */
    protected boolean mLoaded = false;
    /**
     * Mapping of a slug to a serie
     */
    protected Map<String, Serie> mMap = new HashMap<String, Serie>();

    /**
     * Constructor
     *
     * @param c    Parent category
     * @param Name of this sub-category
     * @param URL  to load information from
     */
    public SubCategory(Category c, String name, String url) {
        mParent = c;
        mName = name;
        mUrl = url;
    }

    /**
     * Add a seie to this sub-category
     *
     * @param s
     */
    public void addSerie(Serie s) {
        mMap.put(s.getSlug(), s);
    }

    /**
     * Clear internal map
     */
    public void clear() {
        mMap.clear();
    }

    /**
     * Get the name
     *
     * @return The name
     */
    public String getName() {
        return mName;
    }

    /**
     * Get the mapping
     *
     * @return The map
     */
    public Map<String, Serie> getMap() {
        return mMap;
    }

    /**
     * Get a serie with a particular SLUG
     *
     * @param name SLUG
     * @return Serie or null
     */
    public Serie getSerie(String name) {
        return mMap.get(name);
    }

    /**
     * Check if this sub-category has loaded
     *
     * @return True if this has loaded
     */
    public boolean isLoaded() {
        return mLoaded;
    }

    public Category getParent() {
        return mParent;
    }

    /**
     * load the information
     */
    public boolean load() {
        if (isLoaded()) return true;

        Extractor e = Extractor.instance();
        if (!e.loadSubCategory(this, mName.isEmpty())) return false;
        mLoaded = true;
        return true;
    }

    /**
     * Get the URL to load from
     *
     * @return URL to load from
     */
    public String getUrl() {
        return mUrl;
    }
}
// EOF
